capture log close
log using visit24-cjt, replace text

//  program:    visit21-cjt.do
//  task:       Data Mgmt and Analysis 
//  project:    Foreign Visits and the Image of National Security Defender
//  author:     Hsin-Hsin Pan
//  date:       June/6/2024

// program setup

version 16.1
clear all 
set linesize 80
  
///////////////////////////////
// Data

clear all
eststo clear
set more off	

use "visit_analysis", clear 

///////////////////////////////
// FIGURE 1: 
// Screenshot for Conjoint Analysis with English Translation above 
// the Original Text in Traditional Chinese
///////////////////////////////
// FIGURE 2: 
// Effects of Visiting Major Powers on the Probability of a Politician 
// Being Preferred as a National Security Defender
// AMCE

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(small)) scale(0.7) ///
		 xtitle("Estimated Average Marginal Component Effects (AMCE)", size(medsmall)) ///
		 xlabel(-0.10(0.05)0.05,labsize(medsmall)) msize(medsmall) ///
		 saving(amce_all.gph, replace)					
		   
// MMs

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus, ///
				est(mm) id(id)
					
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
		 ci(( 5 6)) keep(*:) xline(0.5, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
     	 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}" ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///		   
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xtitle("Estimated Marginal Means (MMs)", size(medsmall)) ///
		 xlabel(0.20(0.05)0.35,labsize(medsmall)) msize(medsmall) ///
		 saving(mm_all.gph, replace)

////////////////////////////////			 
// FIGURE 3: 
// Probabilities of Politicians Being Preferred as a National Security Defender,
// Ranked by Centile

eststo clear

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus, ///
		   cluster(id)

predict fitted if e(sample)

sort fitted
					
collapse id p_gender p_party p_edu p_ch p_us p_consensus, by(fitted)

centile fitted, centile(10(10)90)

save "fig3.dta", replace

clear all
use "fig3.dta", clear

twoway scatter centile percentile, legend(off) yla(1/9, ang(h) grid val noticks) ///
               mc(black) ms(X) msize(tiny) ml(centile) mlabposition(3) mlcolor(black) || /// 
               rcap upper lower percentile, lc(black) ///
			   scheme(s1color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
			   xtitle("Hypothetical Profiles within Each Centile", size(small)) ///
			   ytitle("Prob. of Being Preferred for National Security Defender by Percentile", size(small)) ///
			   ylabel(20(5)35,labsize(small) labcolor(black)) ///
			   xlabel(0(10)100, labsize(small) labcolor(black)) ///
			   msize(small) mcolor(black) ///
			   saving(fig3.gph, replace) 
			   
////////////////////////////////				 
// FIGURE 4: 
// Effects of Politician Attributes on the Probability of Being Preferred 
// as a National Security Defender, By Respondent's Perceptions of 
// Taiwan's Relations with the U.S. and China (AMCEs)
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with U.S.
// AMCEs
// Negative

clear all
eststo clear
set more off	

use "visit_analysis", clear 

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus ///
				if r_ustw3==0, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.10(0.05)0.05,labsize(medsmall)) msize(medsmall) ///
		 title("Negative", size(large)) /// 
		 saving(amce_ustw_negative.gph, replace)					
		 
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with U.S.
// AMCEs
// Lukewarm

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus ///
				if r_ustw3==1, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit with any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.10(0.05)0.05,labsize(medsmall)) msize(medsmall) ///
		 title("Lukewarm", size(large)) ///
		 saving(amce_ustw_lukewarm.gph, replace)			
		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with U.S.
// AMCEs
// Positive

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus ///
				if r_ustw3==2, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.10(0.05)0.05,labsize(medsmall)) msize(medsmall) ///
		 title("Positive", size(large)) ///
		 saving(amce_ustw_positive.gph, replace)	   

//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with China
// AMCEs
// Negative

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus ///
				if r_chtw3==0, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.10(0.05)0.05,labsize(medsmall)) msize(medsmall) ///
		 title("Negative", size(large)) /// 
		 saving(amce_chtw_negative.gph, replace)		
		   		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with China
// AMCEs		   
// Lukewarm

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus ///
				if r_chtw3==1, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.10(0.05)0.05,labsize(medsmall)) msize(medsmall) ///
		 title("Lukewarm", size(large)) /// 
		 saving(amce_chtw_lukewarm.gph, replace)		
		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with China
// AMCEs
// Positive

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus ///
				if r_chtw3==2, ///
				base(0 1 1 0 0 0) ///
				est(amce) id(id)
		
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"
				
coefplot matrix(all_results[,1]), bylabel(Overall) ||, ///
         ci(( 5 6)) keep(*:) xline(0, lpattern(-) lcolor(black%70)) ///
		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
		 mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///		   
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(medsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.10(0.05)0.10,labsize(medsmall)) msize(medsmall) ///
		 title("Positive", size(large)) /// 
		 saving(amce_chtw_positive.gph, replace)	

///////////////////////////////////
// TABLE 1: 
// Attributes and Values Used in Politician Profiles for the Conjoint Experiment
///////////////////////////////////
// TABLE 2: 
// Diplomatic Visits and Estimated Probability of Politician Profiles 
// Being Preferred as National Security Defender
////////////////////
// Neither US or China
// Mean: 25.2%

eststo clear

use "visit_analysis", clear 

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus ///
           if p_ch==0 & p_us==0, cluster(id)

predict fitted if e(sample)

sum fitted, detail

////////////////////
// China only
// Mean: 26.1%

eststo clear
drop fitted

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus ///
           if p_ch==1 & p_us==0, cluster(id)

predict fitted if e(sample)

sum fitted, detail

////////////////////
// US only
// Mean: 27.8%

eststo clear
drop fitted

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus ///
           if p_ch==0 & p_us==1, cluster(id)

predict fitted if e(sample)

sum fitted, detail

////////////////////
// Both US and China
// Mean: 30.0%

eststo clear

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus ///
           if p_ch==1 & p_us==1, cluster(id)

predict fitted if e(sample)

sum fitted, detail
	 
///////////////////////////////////
// APPENDIX 1: 
// Questionnaire for "Diplomatic Visits to Major Powers and Politicians' Image 
// as National Security Defenders in Minor Powers" 
// (English Translation Follows the Original Text in Traditional Chinese)


///////////////////////////////
// Appendix 2
// Percentage of Politicians' Attributes in 144 Profiles within Each Decile
// count in each decile

eststo clear
drop fitted

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus, cluster(id)

predict fitted

xtile decile = fitted, nq(10)

tab p_gender, generate(d_gender)
tab p_party, generate(d_party)
tab p_edu, generate(d_edu)
tab p_ch, generate(d_ch)
tab p_us, generate(d_us)
tab p_consensus, generate(d_consensus)

collapse (mean) decile d_gender* d_party* d_edu* d_ch* d_us* d_consensus*, by(fitted)

gsort -fitted

tab1 d_gender1-d_consensus2 if decile==10
tab1 d_gender1-d_consensus2 if decile==9
tab1 d_gender1-d_consensus2 if decile==8
tab1 d_gender1-d_consensus2 if decile==7
tab1 d_gender1-d_consensus2 if decile==6
tab1 d_gender1-d_consensus2 if decile==5
tab1 d_gender1-d_consensus2 if decile==4
tab1 d_gender1-d_consensus2 if decile==3
tab1 d_gender1-d_consensus2 if decile==2
tab1 d_gender1-d_consensus2 if decile==1

tab decile, missing

// percentage in each decile

clear all

use "visit_analysis", clear 

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus, ///
		   cluster(id)

predict fitted

xtile decile = fitted, nq(10)

tab p_gender, generate(d_gender)
tab p_party, generate(d_party)
tab p_edu, generate(d_edu)
tab p_ch, generate(d_ch)
tab p_us, generate(d_us)
tab p_consensus, generate(d_consensus)

gsort -fitted

collapse (mean) fitted d_gender* d_party* d_edu* d_ch* d_us* d_consensus*, by(decile)

gsort -fitted

///////////////////////////////////
// APPENDIX 3: 
// Estimated Probability of a Politician Being Preferred for 
// National Security Defender for 144 Hypothetical Profiles in Centile

clear all

use "visit_analysis", clear 

reg choice i.p_gender i.p_party i.p_edu i.p_ch i.p_us i.p_consensus, ///
		   cluster(id)

predict fitted

xtile decile = fitted, nq(10)

tab p_gender, gen(d_gender)
tab p_party, gen(d_party)
tab p_edu, gen(d_edu)
tab p_ch, gen(d_ch)
tab p_us, gen(d_us)
tab p_consensus, gen(d_consensus)

collapse (mean) decile d_gender* d_party* d_edu* d_ch* d_us* d_consensus*, by(fitted)

gsort -fitted

///////////////////////////////
// APPENDIX 4
// Ordinary least square regression (OLS) models 
////////////////
// FIGURE 2: 
// Effects of Visiting Major Powers on the Probability of a Politician 
// Being Preferred as a National Security Defender
// AMCE

reg choice p_gender p_party p_edu p_ch p_us p_consensus, ///
		   cluster(id)
eststo m1

////////////////			 
// FIGURE 4: 
// Effects of Politician Attributes on the Probability of Being Preferred 
// as a National Security Defender, By Respondent's Perceptions of 
// Taiwan's Relations with the U.S. and China (AMCEs)
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with U.S.
// AMCEs
// Negative

reg choice p_gender p_party p_edu p_ch p_us p_consensus ///
             if r_ustw3==0, cluster(id)
eststo m2
		   		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with U.S.
// AMCEs	   
// Lukewarm

reg choice p_gender p_party p_edu p_ch p_us p_consensus ///
             if r_ustw3==1, cluster(id)
eststo m3
		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with U.S.
// AMCEs
// Positive

reg choice p_gender p_party p_edu p_ch p_us p_consensus ///
             if r_ustw3==2, cluster(id)
eststo m4 		   

//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with China
// AMCEs
// Negative

reg choice p_gender p_party p_edu p_ch p_us p_consensus ///
             if r_chtw3==0, cluster(id)
eststo m5
		   		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with China
// AMCEs		   
// Lukewarm

reg choice p_gender p_party p_edu p_ch p_us p_consensus ///
             if r_chtw3==1, cluster(id)
eststo m6			 
		   
//////////////////////////////////// 
// Respondent's Perceived Taiwan's Relations with China
// AMCEs
// Positive

reg choice p_gender p_party p_edu p_ch p_us p_consensus ///
           if r_chtw3==2, cluster(id)
eststo m7

esttab m1 m2 m3 m4 m5 m6 m7 using "tab.csv", replace label nodepvar nonumber ///
	b(2) se(2) ar2 scalar(ll)    ///
	sfmt(%12.2f) brackets ///
    star(* 0.05 ** 0.01 *** 0.001) 	

////////////////////////////////////// 
// APPENDIX 5
// Estimated Probability of Politicians Being Preferred for 
// National Security Defender by Respondent's Gender
// MM

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus, ///
				est(mm) id(id) subgroup(r_gender) 
					
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"

local subgroup `e(subgroupvar)'
local subgroups `e(subgroup_labels)'
local count 1
local pooleddf 0
foreach sub of local subgroups {
    if `count'>2 {
	    di as error "Code only set up for 2 subgroups, aborting to prevent errors"
		exit 198
	}
	local pooleddf = `pooleddf' + `e(df_r_`sub')'
	matrix results`count' = e(results_`sub')
	local label`count' "`sub'"
	local count = `count' + 1
}	

local rows = rowsof(results1)
matrix diff = J(`rows',6,.)
local rnames : rownames results1
local cnames : colnames results1
local reqs : roweq results1
matrix rownames diff = `rnames'
matrix roweq diff = `reqs'
matrix colnames diff = `cnames'

forvalues rownum = 1/`rows' {
	local cdiff = results2[`rownum',1] - results1[`rownum',1]
	local pooledse = sqrt(results2[`rownum',2]^2 + results1[`rownum',2]^2)
	local tstat = (results2[`rownum',1] - results1[`rownum',1])/`pooledse'
	local tfactor = invttail(`pooleddf', 0.025)
	local pvalue = 2*ttail(`pooleddf',abs(`tstat'))
	local lb = `cdiff' - `pooledse'*`tfactor'
	local ub = `cdiff' + `pooledse'*`tfactor'
	matrix diff[`rownum',1]=`cdiff', `pooledse', `tstat', `pvalue', `lb', `ub'
}

di as text "Differences"
matlist diff, border(rows) rowtitle(Variable / Levels) ///
			format(%8.4f) twidth(25) underscore

coefplot  matrix(results1[,1]), bylabel("{bf:Female}") ||  ///
          matrix(results2[,1]), bylabel("{bf:Male}") ||  ///
		  matrix(diff[,1]), bylabel("{bf:Difference}") || , ///
          ci(( 5 6)) keep(*:) ///
  		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
          mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(vsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.1(0.1)0.4,labsize(tiny)) msize(vsmall)  ///
		 saving(mm_gender.gph, replace)	

///////////////////////////////////
// Appendix 6
// Estimated Probability of Politicians Being Preferred for 
// National Security Defender by Respondent's Birth Year
// MM

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus, ///
				    est(mm) id(id) subgroup(r_age2) 
					
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"

local subgroup `e(subgroupvar)'
local subgroups `e(subgroup_labels)'
local count 1
local pooleddf 0
foreach sub of local subgroups {
    if `count'>2 {
	    di as error "Code only set up for 2 subgroups, aborting to prevent errors"
		exit 198
	}
	local pooleddf = `pooleddf' + `e(df_r_`sub')'
	matrix results`count' = e(results_`sub')
	local label`count' "`sub'"
	local count = `count' + 1
}	

local rows = rowsof(results1)
matrix diff = J(`rows',6,.)
local rnames : rownames results1
local cnames : colnames results1
local reqs : roweq results1
matrix rownames diff = `rnames'
matrix roweq diff = `reqs'
matrix colnames diff = `cnames'

forvalues rownum = 1/`rows' {
	local cdiff = results2[`rownum',1] - results1[`rownum',1]
	local pooledse = sqrt(results2[`rownum',2]^2 + results1[`rownum',2]^2)
	local tstat = (results2[`rownum',1] - results1[`rownum',1])/`pooledse'
	local tfactor = invttail(`pooleddf', 0.025)
	local pvalue = 2*ttail(`pooleddf',abs(`tstat'))
	local lb = `cdiff' - `pooledse'*`tfactor'
	local ub = `cdiff' + `pooledse'*`tfactor'
	matrix diff[`rownum',1]=`cdiff', `pooledse', `tstat', `pvalue', `lb', `ub'
}

di as text "Differences"
matlist diff, border(rows) rowtitle(Variable / Levels) ///
			format(%8.4f) twidth(25) underscore

coefplot  matrix(results1[,1]), bylabel("{bf:After 1978}") ||  ///
          matrix(results2[,1]), bylabel("{bf:Before 1978}") ||  ///
		  matrix(diff[,1]), bylabel("{bf:Difference}") || , ///
          ci(( 5 6)) keep(*:) ///
  		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
          mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(vsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.1(0.1)0.4,labsize(tiny)) msize(vsmall)  ///
		 saving(mm_age.gph, replace)	
		 
///////////////////////////////////
// Appendix 7
// Estimated Probability of Politicians Being Preferred for 
// National Security Defender by Respondent's Education
// MMs

eststo clear

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus, ///
				    est(mm) id(id) subgroup(r_edu) 
					
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"

local subgroup `e(subgroupvar)'
local subgroups `e(subgroup_labels)'
local count 1
local pooleddf 0
foreach sub of local subgroups {
    if `count'>2 {
	    di as error "Code only set up for 2 subgroups, aborting to prevent errors"
		exit 198
	}
	local pooleddf = `pooleddf' + `e(df_r_`sub')'
	matrix results`count' = e(results_`sub')
	local label`count' "`sub'"
	local count = `count' + 1
}	

local rows = rowsof(results1)
matrix diff = J(`rows',6,.)
local rnames : rownames results1
local cnames : colnames results1
local reqs : roweq results1
matrix rownames diff = `rnames'
matrix roweq diff = `reqs'
matrix colnames diff = `cnames'

forvalues rownum = 1/`rows' {
	local cdiff = results2[`rownum',1] - results1[`rownum',1]
	local pooledse = sqrt(results2[`rownum',2]^2 + results1[`rownum',2]^2)
	local tstat = (results2[`rownum',1] - results1[`rownum',1])/`pooledse'
	local tfactor = invttail(`pooleddf', 0.025)
	local pvalue = 2*ttail(`pooleddf',abs(`tstat'))
	local lb = `cdiff' - `pooledse'*`tfactor'
	local ub = `cdiff' + `pooledse'*`tfactor'
	matrix diff[`rownum',1]=`cdiff', `pooledse', `tstat', `pvalue', `lb', `ub'
}

di as text "Differences"
matlist diff, border(rows) rowtitle(Variable / Levels) ///
			format(%8.4f) twidth(25) underscore

coefplot  matrix(results1[,1]), bylabel("{bf:No College}") ||  ///
          matrix(results2[,1]), bylabel("{bf:College}") ||  ///
		  matrix(diff[,1]), bylabel("{bf:Difference}") || , ///
          ci(( 5 6)) keep(*:) ///
  		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
          mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(vsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.1(0.1)0.4,labsize(tiny)) msize(vsmall)  ///
		 saving(mm_edu.gph, replace)	
 
///////////////////////////////////
// Appendix 8
// Estimated Probability of Politicians Being Preferred for 
// National Security Defender by Respondent's City of Residence
// MMs

conjoint choice p_gender p_party p_edu p_ch p_us p_consensus, ///
				    est(mm) id(id) subgroup(r_reside) 
					
matrix all_results = e(results)				 
display _asis "`e(graph_code)'"

local subgroup `e(subgroupvar)'
local subgroups `e(subgroup_labels)'
local count 1
local pooleddf 0
foreach sub of local subgroups {
    if `count'>2 {
	    di as error "Code only set up for 2 subgroups, aborting to prevent errors"
		exit 198
	}
	local pooleddf = `pooleddf' + `e(df_r_`sub')'
	matrix results`count' = e(results_`sub')
	local label`count' "`sub'"
	local count = `count' + 1
}	

local rows = rowsof(results1)
matrix diff = J(`rows',6,.)
local rnames : rownames results1
local cnames : colnames results1
local reqs : roweq results1
matrix rownames diff = `rnames'
matrix roweq diff = `reqs'
matrix colnames diff = `cnames'

forvalues rownum = 1/`rows' {
	local cdiff = results2[`rownum',1] - results1[`rownum',1]
	local pooledse = sqrt(results2[`rownum',2]^2 + results1[`rownum',2]^2)
	local tstat = (results2[`rownum',1] - results1[`rownum',1])/`pooledse'
	local tfactor = invttail(`pooleddf', 0.025)
	local pvalue = 2*ttail(`pooleddf',abs(`tstat'))
	local lb = `cdiff' - `pooledse'*`tfactor'
	local ub = `cdiff' + `pooledse'*`tfactor'
	matrix diff[`rownum',1]=`cdiff', `pooledse', `tstat', `pvalue', `lb', `ub'
}

di as text "Differences"
matlist diff, border(rows) rowtitle(Variable / Levels) ///
			format(%8.4f) twidth(25) underscore

coefplot  matrix(results1[,1]), bylabel("{bf:Others}") ||  ///
          matrix(results2[,1]), bylabel("{bf:6 Municipalities}") ||  ///
		  matrix(diff[,1]), bylabel("{bf:Difference}") || , ///
          ci(( 5 6)) keep(*:) ///
  		 scheme(s2color) graphregion(fcolor(white) ilcolor(white) lcolor(white)) ///
          mcolor(black) ciopts(lcolor(black)) ///
		 coeflabels( ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
           Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown" ///
		   Hidden="Hidden" Shown="Shown", ///
		   labsize(medsmall)) ///
		 eqlabels(  ///
		   "{bf:Gender}" ///
		   "{bf:Party}" ///
		   "{bf:Education}" ///
		   "{bf:Visit any senior Chinese officials}"  ///
		   "{bf:Visit any senior US officials}" ///
		   "{bf:Cross-Strait relations policy}", ///
		 asheadings labsize(medsmall)) ///
		 mlabel mlabsize(vsmall) mlabposition(12) format(%9.2f) ///
		 mlabcolor(black) ///
		 byopts(graphregion(col(white)) cols(3)) ///
		 subtitle(, fcolor (gs15) size(medsmall)) scale(0.7) ///
		 xlabel(-0.1(0.1)0.4,labsize(tiny)) msize(vsmall)  ///
		 saving(mm_resid.gph, replace)	
 				 
//////////////////////////////////////////////////////////////////// 	 
// Close log file & exit do-file
    
    log close
    exit	
	